/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.prashant.sqlduniya.editor.document;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

/**
 *
 * @author prashant
 */
public class DelayedDocumentListener implements DocumentListener, ActionListener{ 
    private Timer timer = null; 
 
    private DocumentListener delegate; 
    private DocumentEvent event; 
 
    public DelayedDocumentListener(DocumentListener delegate){ 
        this(delegate, 3000); 
    } 
 
    public DelayedDocumentListener(DocumentListener delegate, int delay){ 
        this.delegate = delegate; 
        timer = new Timer(delay, this); 
    } 
 
    private void documentChanged(DocumentEvent de){ 
        timer.restart(); // Restart the timer which starts the calls back after the specified delay. 
        event = de; 
    } 
 
    /*-------------------------------------------------[ DocumentListener ]---------------------------------------------------*/ 
 
    public void changedUpdate(DocumentEvent e){ 
        documentChanged(e); 
    } 
 
    public void insertUpdate(DocumentEvent e){ 
        documentChanged(e); 
    } 
 
    public void removeUpdate(DocumentEvent e){ 
        documentChanged(e); 
    } 
 
    /*-------------------------------------------------[ ActionListener ]---------------------------------------------------*/ 
 
    public void actionPerformed(ActionEvent e){ 
        if (timer.isRunning()) 
            timer.stop(); 
 
        DocumentEvent.EventType eventType = event.getType(); 
        if(eventType==DocumentEvent.EventType.INSERT) 
            delegate.insertUpdate(event); 
        else if(eventType==DocumentEvent.EventType.REMOVE) 
            delegate.removeUpdate(event); 
        if(eventType==DocumentEvent.EventType.CHANGE) 
            delegate.changedUpdate(event); 
    } 
}
